#!/usr/bin/make -f
# $Id: rules,v 1.6 2007-08-06 16:30:01 ianb Exp $
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

LDDEFS := -Wl,-z,defs -Wl,--as-needed

CFLAGS += $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS += $(shell dpkg-buildflags --get CXXFLAGS)
CPPFLAGS += $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	CROSS :=
else
	CROSS := CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CONFIGUREARGS=--with-debug
else
	CONFIGUREARGS=
endif

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir

	if [ ! -f MCONFIG ]; then \
		./configure $(CONFIGUREARGS); \
		sed -e 's;^CFLAGS=\(.*\)$$;CFLAGS= -Ddebian -D_GNU_SOURCE -DACCEPT_USERVAR -g $(CFLAGS) $(CPPFLAGS) \1;' \
		    -e 's;^CXXFLAGS=\(.*\)$$;CXXFLAGS= -Ddebian -D_GNU_SOURCE -DACCEPT_USERVAR -g $(CXXFLAGS) $(CPPFLAGS) \1;' \
		    -e 's;^LDFLAGS=\(.*\)$$;& $(LDDEFS) $(LDFLAGS);' \
		    MCONFIG > MCONFIG.new; \
		mv MCONFIG.new MCONFIG; \
	fi
	$(MAKE) $(CROSS)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	[ ! -f MCONFIG ] || $(MAKE) distclean
	rm -f debian/telnetd-ssl.dirs debian/telnetd-ssl.postinst debian/telnetd-ssl.postrm debian/telnetd-ssl.prerm

	dh_clean

install: install-stamp
install-stamp: build-stamp
	cp debian/telnetd.dirs debian/telnetd-ssl.dirs
	cp debian/telnetd.postinst debian/telnetd-ssl.postinst
	cp debian/telnetd.postrm debian/telnetd-ssl.postrm
	cp debian/telnetd.prerm debian/telnetd-ssl.prerm
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) -C telnet INSTALLROOT=$(CURDIR)/debian/telnet-ssl MANDIR=/usr/share/man \
		install
#	mv debian/tmp/usr/bin/telnet debian/tmp/usr/bin/telnet.netkit
#	mv debian/tmp/usr/share/man/man1/telnet.1 \
#	   debian/tmp/usr/share/man/man1/telnet.netkit.1
	cp telnet/README debian/telnet-ssl/usr/share/doc/telnet-ssl/README.telnet
	cp telnet/README.old debian/telnet-ssl/usr/share/doc/telnet-ssl/README.telnet.old
	$(MAKE) -C telnetd INSTALLROOT=$(CURDIR)/debian/telnetd-ssl \
		MANDIR=/usr/share/man install
	cp telnetlogin/telnetlogin.8 debian/telnetd-ssl/usr/share/man/man8
	cp telnetlogin/telnetlogin debian/telnetd-ssl/usr/lib
	cp debian/openssl.cnf debian/telnetd-ssl/etc/telnetd-ssl

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installmenu
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary
